<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>注册页面</title>
    <link href="css/regist.css" rel="stylesheet" type="text/css" />
    <script src="js/jquery-1.11.3.js"></script>
  </head>

  <body>
    <div class="main_div">
      <div class="title">用户注册</div>
      <div class="input_text_div">
        <i class="username"></i>
        <input type="text" id="UserName" placeholder="用户账号  (用作登录)" />
      </div>
      <div class="input_text_div">
        <i class="userpassword"></i>
        <input type="text" id="UserPassword" placeholder="用户密码" />
      </div>
      <div class="input_text_div">
        <i class="phone"></i>
        <input type="text" id="Phone" placeholder="手机号" />
      </div>
      <div class="input_text_div">
        <i class="nickname"></i>
        <input type="text" id="NickName" placeholder="昵称" />
      </div>
      <div class="input_text_div">
        <i class="email"></i>
        <input type="text" id="Email" placeholder="电子邮箱" />
      </div>
      <div class="input_radio_div">
        <i class="sex"></i>
        <label for="sex_boy"
          ><input id="sex_boy" type="radio" name="sex" value="0" checked /><span
            >男</span
          ></label
        >
        <label for="sex_girl"
          ><input id="sex_girl" type="radio" name="sex" value="1" /><span
            >女</span
          ></label
        >
      </div>
      <div class="butten_div">
        <a onclick="regist()">注册</a>
        <i>已有账号?<a onclick="login()">点击此处</a>登录</i>
      </div>
    </div>
    <script>
      function login() {
        location.href = "login.html";
      }
      function regist() {
        var UserName = $("#UserName").val();
        var UserPassword = $("#UserPassword").val();
        var Phone = $("#Phone").val();
        var NickName = $("#NickName").val();
        var Email = $("#Email").val();
        if (UserName == null || UserName.trim() == "") {
          alert("用户名不能为空");
          return;
        }
        if (UserPassword == null || UserPassword.trim() == "") {
          alert("密码不能为空");
          return;
        }
        if (Phone == null || Phone.trim() == "") {
          alert("手机号不能为空");
          return;
        }
        if (NickName == null || NickName.trim() == "") {
          alert("昵称不能为空");
          return;
        }
        if (Email == null || Email.trim() == "") {
          alert("邮箱不能为空");
          return;
        }
        var formData = new FormData();
        formData.append("username", UserName);
        formData.append("userpassword", UserPassword);
        formData.append("phone", Phone);
        formData.append("nickname", NickName);
        formData.append("email", Email);
        formData.append("sex", $("input[name='sex']:checked").val());
        console.log($("input[name='sex']:checked").val());
        $.ajax({
          type: "post",
          url: "http://localhost:8080/user/register",
          dataType: "JSON",
          data: formData,
          contentType: false,
          processData: false,
          success: function (mes) {
            if (mes.code == "200") {
              alert("注册成功");
              location.href = "login.html";
            } else {
              alert(mes.mes);
            }
          },
          false: function (mes) {
            console.log(mes);
          },
        });
      }
    </script>
  </body>
</html>
